package com.tianqu.tms.www.cardect.action;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TmsDetectPic;
import com.tianqu.tms.www.cardect.actionform.CarImgInputActionForm;
import com.tianqu.tms.www.cardect.viewhelper.CarSeniorDetectViewHelper;

/**
 * @author tianshifu
 * 
 * 
 * 对图片进行上传和确认操作
 */
public class CarSDImageResultAction extends TmsAction {

	private static final String IMGTYPE = "imgType";//1：车辆整体图片
	private static final String IMGKEY = "imgKey";//图片位置key
	private static final String IMGLIST = "imgList";//用于存放图片对象的list
	private static final String SEEPIC = "seePic";
	private static final String UPPIC = "upPic";
	private static final String SENIOR_DETECT_INPUT_DATA = "senior_detect_input_data";//检测录入信息

	
	@SuppressWarnings("unchecked")
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
	
		String imgType = (String)uc.getAttribute(IMGTYPE);
		String imgKey = (String)uc.getAttribute(IMGKEY);
		String forward = "";
		
		//从form中
		CarImgInputActionForm carImgForm = (CarImgInputActionForm)form;
		/****************************单独写一个用于图片上传下载显示的viewhelp，在viewhelp中定义一个车辆ID************************************/
		CarSeniorDetectViewHelper viewhelp = (CarSeniorDetectViewHelper)uc.getAttribute(SENIOR_DETECT_INPUT_DATA);
		//显示图片的button
		if(carImgForm.getSeePicButton() != null){
			
			FormFile formfil =  carImgForm.getImgPath();
			String path =ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOADTMP_CONTEXT_ROOT_KEY));
		    String img = System.currentTimeMillis()+".jpg";//图片名称
			FileOutputStream fileOutput = new FileOutputStream(new File(path+img) );
		    fileOutput.write(formfil.getFileData());
            fileOutput.flush();
			viewhelp.setSeePic(ImageUtils.getImageUrl(Registry.getConfig(TmsConstants.UPLOADTMP_CONTEXT_ROOT_KEY) + img));
			//前台显示出隐藏部分的图片
			viewhelp.setSeePicFlag(true);
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
			//返回当前页面 要对form的值重新set
			carImgForm.setSeePicButton(null);
			request.setAttribute(TmsConstants.VIEWHELP, viewhelp);
			forward = SEEPIC;
		}else{

			//把一组数据打包存如entity中
			TmsDetectPic entity  = TmsDetectPic.getInstance();
			entity.setImgKey(imgKey);
			entity.setImgType(Integer.parseInt(imgType));
			entity.setImgPath(viewhelp.getSeePic());//这个Path需要处理一下
			if("1".equals(imgKey)){
				viewhelp.setImg01(viewhelp.getSeePic());
			}else if("2".equals(imgKey)){
				viewhelp.setImg02(viewhelp.getSeePic());
			}else if("3".equals(imgKey)){
				viewhelp.setImg03(viewhelp.getSeePic());
			}else if("4".equals(imgKey)){
				viewhelp.setImg04(viewhelp.getSeePic());
			}else if("5".equals(imgKey)){
				viewhelp.setImg05(viewhelp.getSeePic());
			}else if("6".equals(imgKey)){
				viewhelp.setImg06(viewhelp.getSeePic());
			}
			//前台不显示隐藏部分的图片
			viewhelp.setSeePicFlag(false);
			//放到一个list中，每次存的时候往里面ADD一下就可以了
			List<TmsDetectPic> list = new ArrayList<TmsDetectPic>();
			if(uc.getAttribute(IMGLIST)!=null)
			{
				list = (List<TmsDetectPic>)uc.getAttribute(IMGLIST);
			}
			
			//每次存入之前将相同位置图片删掉
			if(list!=null)
			{
				for(int i=0;i<list.size();i++)
				{
					TmsDetectPic picEntity = list.get(i);
					if(picEntity.getImgType() == Integer.parseInt(imgType))
					{
						if(picEntity.getImgKey().equals(imgKey))
						{
							//移除
							list.remove(picEntity);
						}
					}
				}
			}
			
			list.add(entity);		
			uc.setAttribute(IMGLIST, list);//按图片位置存在uc中
			uc.removeAttribute(IMGTYPE);
			uc.removeAttribute(IMGKEY);
			request.setAttribute(TmsConstants.VIEWHELP, uc.getAttribute(TmsConstants.VIEWHELP));
			forward = UPPIC;
			
		}
		return mapping.findForward(forward);
	}

}
